package users;

import java.io.Serializable;

import networkedmodules.Module;
import networkedmodules.ModuleGroup;
import networkedmodules.Update;

@SuppressWarnings("serial")
public class NewUserUpdate extends Update implements Serializable
{
	UserGroup userGroup;
	public NewUserUpdate(String key, UserGroup userGroup)
	{
		setKey(key);
		this.userGroup = userGroup;
	}
	
	public void performUpdate(Module module)
	{
		if(module.getModuleGroup().getType() == ModuleGroup.HOST)
		{
			UserGroup group = (UserGroup)module;
			group.addAnother(userGroup.you);
			group.getModuleGroup().sendUpdate(new NewUserUpdate(group.getKey(), group));
		}
		else if(module.getModuleGroup().getType() == ModuleGroup.CLIENT)
		{
			UserGroup group = (UserGroup)module;
			group.others.add(userGroup.you);
			for(User u : userGroup.others)
			{
				System.out.println(group);
				if(u != null && !u.equals(group.you))
					group.others.add(u);
			}
		}
	}
}
